package org.NooLab.glue.net.http.rest.resources;

import java.io.IOException;

import org.NooLab.glue.net.http.rest.resources.BaseResource.MessageSupply;
import org.NooLab.utilities.logging.PrintLog;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.data.Parameter;
import org.restlet.data.Status;
import org.restlet.engine.http.header.HeaderConstants;
import org.restlet.ext.xml.DomRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.Post;




public class TaskReceivingResource extends BaseResource{
	
	PrintLog out = new PrintLog(4,true) ;
	
	
	public TaskReceivingResource(){
		 
	}
	
	@Get
	public String DataHandlerGet(Representation entity){
		String taskMsg ="WXYY" ;
		
												out.print(2, "handling task transmission to RECEPTOR") ;
		 
		
		// get param from request
		// String fooParam = getQuery().getFirstValue('foo');  
        // getQuery().getValues("userId")
		// getRequest().getAttributes().get('page');

		String msgText="", role="" ;
		MessageSupply msgSupply;
		
		
		role = this.getRegistrationTarget(); // SOURCE, RECEPTOR
		msgSupply = prepareMessageRouting();
		
		// we now need to fetch the transaction data
		
		
		try {
		
			/// taskMsg = entity.getText();

												out.printErr(2, "TaskReceivingResource, GET-resource, request text : \n\r" + taskMsg);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		serveAndGetReturn( msgSupply, taskMsg);
		
		// getResponse().commit();
		
		return regResult;
		// return taskMsg;
	}
 
	@Post 
	public String DataHandlerPost(Representation entity){   
		
		String msgText="" ;
		MessageSupply msgSupply;
		
		
		
		msgSupply = prepareMessageRouting();

		try {
			
												// outx out.print(4,"Restlet-Server (1), TaskReceivingResource, DataHandlerPost(task) -> ID => "+this.toString() );
			
			msgText = entity.getText();
			
			// this should return the task package itself,  if we have a request like
			// <transaction> <request content="resource" type="task"/>
			
		} catch (Exception e) {
			e.printStackTrace();
		}
												out.print(5,"received message in Task receiving resource : \n\r" + msgText);
		serveAndGetReturn( msgSupply, msgText);
		
		getResponse().commit();
		
		return regResult;
		
		// String getmsg = "response by '"+this.toString()+"', Confirmation for post id=21323435, response from server" ;
		
		 
	}
	
	protected void addResponseHeader(String name, String value) {
		
	    Form responseHeaders = (Form)getResponse().getAttributes().get(HeaderConstants.ATTRIBUTE_HEADERS);
	    if (responseHeaders == null) {
	        responseHeaders = new Form();
	        getResponse().getAttributes().put(HeaderConstants.ATTRIBUTE_HEADERS, responseHeaders);
	    }
	    responseHeaders.add(new Parameter(name, value));
	}
	

	 /*
	  * getResponse().setEntity() method from acceptRepresentation() will accept the new FileRepresentation
	  * 
	  */

}
